package com.example.demo.leetcode.top;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你二叉树的根节点 root ，返回它节点值的 前序 遍历。
 *
 *
 *
 * 示例 1：
 **/
public class Btree_二叉树的前序遍历 {

 public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list =new ArrayList<>();
        preDfs(list,root);
        return list;
    }

    public void preDfs(List<Integer> list,TreeNode root){
        if(root == null){
            return;
        }
        list.add(root.val);
        preDfs(list,root.left);
        preDfs(list,root.right);
    }

}
